<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17
 * Time: 14:12
 */

namespace app\api\controller;

use think\Db;
header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");

class PlayPk10 extends Base{

    public function index(){

        $czid = input("czid",0);
        $ret = array(
            'result'=>0,
            'ResultMsg' => ''
        );
        if( empty($czid) ){
            $ret['ResultMsg'] = '请求参数错误';
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $this->qihaoNext($czid);
        $now = time();
        $next = $this->nextResult($czid);
        $datas = [];
        $datas['current']['next_no'] = $next['num'];
        $datas['current']['next_time'] = $next['time'];

//        $fp_time = Db::name('caipai_list')->where(['id'=>$czid])->value('cp_fp_time');
//        if(!empty($datas['current']['next_time'])) $datas['current']['next_time'] = intval($datas['current']['next_time'])-intval($fp_time);

        $current = $this->currentHasResult($czid,$now);
        $datas['current']['current_no'] = $current['num'];
        $datas['current']['current_time'] = $current['time'];
        $current_kjjg = str_split($current['kjjg'],2);
        $datas['current']['current_kjjg'] = $current_kjjg;

        $datas['current']['_1'] = intval($current_kjjg[0]) > intval($current_kjjg[9]) ? '龙' : '虎';
        $datas['current']['_2'] = intval($current_kjjg[1]) > intval($current_kjjg[8]) ? '龙' : '虎';
        $datas['current']['_3'] = intval($current_kjjg[2]) > intval($current_kjjg[7]) ? '龙' : '虎';
        $datas['current']['_4'] = intval($current_kjjg[3]) > intval($current_kjjg[6]) ? '龙' : '虎';
        $datas['current']['_5'] = intval($current_kjjg[4]) > intval($current_kjjg[5]) ? '龙' : '虎';
        $datas['current']['_6'] = intval($current_kjjg[0]) + intval($current_kjjg[1]);
        $datas['current']['_7'] = 11==$datas['current']['_6'] ? "和" : (11 < $datas['current']['_6'] ? '大':'小');
        $datas['current']['_8'] = $datas['current']['_6'] % 2 ? '单' : '双' ;

        $before = $this->currentResult($czid,$now);
        $datas['current']['before_no'] = $before['num'];
        $datas['current']['amount'] = Db::name("time")->where(['czid'=>$czid,'jztime'=>['>=',date('H:i:s',time())]])->count();

        $list = Db::name('kjjg_list')->where('czid',$czid)->where('kjjg','<>','0')->field("qihao,kjjg,jztime")->order('jztime','DESC')->paginate(10)->each(function($v){

            $v['kjjg'] = str_split($v['kjjg'],2);
            $v['_1'] = intval($v['kjjg'][0]) > intval($v['kjjg'][9]) ? '龙' : '虎';
            $v['_2'] = intval($v['kjjg'][1]) > intval($v['kjjg'][8]) ? '龙' : '虎';
            $v['_3'] = intval($v['kjjg'][2]) > intval($v['kjjg'][7]) ? '龙' : '虎';
            $v['_4'] = intval($v['kjjg'][3]) > intval($v['kjjg'][6]) ? '龙' : '虎';
            $v['_5'] = intval($v['kjjg'][4]) > intval($v['kjjg'][5]) ? '龙' : '虎';
            $v['_6'] = intval($v['kjjg'][0]) + intval($v['kjjg'][1]);
            $v['_7'] = 11 == $v['_6'] ? '和' : (11 < $v['_6'] ? '大':'小');
            $v['_8'] = $v['_6'] % 2 ? '单' : '双' ;
            unset($v['jztime']);
            return $v;
        })->toArray();
        $list && $list = $list['data'];
        $datas['history'] = $list;
        $ret['result'] = 1;
        $ret['datas'] = $datas;
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
    public function currentPeriod(){

        $czid = input("czid",0);
        $ret = array(
            'result'=>0,
            'ResultMsg' => ''
        );
        if( empty($czid) ){
            $ret['ResultMsg'] = '请求参数错误';
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $this->qihaoNext($czid);
        $before_redis = $this->currentResult($czid,time());
        $datas = [
            'kjjg' => [],
            'qihao' => '',
            'jztime' => '',
            '_1' => '',
            '_2' => '',
            '_3' => '',
            '_4' => '',
            '_5' => '',
            '_6' => '',
            '_7' => '',
            '_8' => '',
        ];
        if( !empty($before_redis['kjjg']) ){
            $datas['kjjg'] = str_split($before_redis['kjjg'],2);
            $datas['qihao'] = $before_redis['num'];
            $datas['jztime'] = $before_redis['time'];
            $datas['_1'] = intval($datas['kjjg'][0]) > intval($datas['kjjg'][9]) ? '龙' : '虎';
            $datas['_2'] = intval($datas['kjjg'][1]) > intval($datas['kjjg'][8]) ? '龙' : '虎';
            $datas['_3'] = intval($datas['kjjg'][2]) > intval($datas['kjjg'][7]) ? '龙' : '虎';
            $datas['_4'] = intval($datas['kjjg'][3]) > intval($datas['kjjg'][6]) ? '龙' : '虎';
            $datas['_5'] = intval($datas['kjjg'][4]) > intval($datas['kjjg'][5]) ? '龙' : '虎';
            $datas['_6'] = intval($datas['kjjg'][0]) + intval($datas['kjjg'][1]);
            $datas['_7'] = 11 == $datas['_6'] ? '和' : (11 < $datas['_6'] ? '大':'小');
            $datas['_8'] = $datas['_6'] % 2 ? '单' : '双' ;
        }
        $ret['result'] = 1;
        $ret['datas'] = $datas;
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
}